wx.cloud.init()
const db = wx.cloud.database()
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showView: false,
    showViews: false,
    course_datas:[],
    courses_datas:[],
    is_empty:"",
    title:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    db.collection('Study_Information').where({
      type:'课程推荐'
    }).get({
      success: function(res) {
        if(res.data.length != 0){
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:false
          })
        }else{
          that.setData({
            is_empty:true
          })
        }
      }
    })
  },

  // 点击more出现排序页面
  click_more1:function(){
    this.setData({
      showView: (!this.data.showView),
    })
  },
  click_more2:function(){
    this.setData({
      showViews: (!this.data.showViews),
    })
  },
  //搜索框搜索内容，按下回车后搜索
  Search_Item:function (e) {
    let title = e.detail.value
    if(!title){
      this.onLoad()
    }
    let that = this
    db.collection('Study_Information')
    .where({
      type:"课程推荐",
      title:db.RegExp({
        regexp: '.*' + title + '.*',//做为关键字进行匹配
        options: 'i',//不区分大小写
      })
    })
    .get({
      success: function(res) {
        if(res.data.length != 0){
          that.setData({
            title:title,
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:false
          })
        }else{
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:true
          })
        }
      }
    })
  },
  //排序函数
  compare: function (property) {
    return function (a, b) {
      var value1 = a[property]
      var value2 = b[property]
      return value2 - value1
    }
  },
  //选择按发布时间排序
  sort_by_time:function (e) {
    this.data.course_datas.sort(this.compare("time"))
    this.setData({
      showView: (!this.data.showView),
      courses_datas:(this.data.course_datas)
    })
  },
  //选择按热度排序
  sort_by_hot:function (e) {
    this.data.course_datas.sort(this.compare("hot"))
    this.setData({
      showView: (!this.data.showView),
      courses_datas:(this.data.course_datas)
    })
    console.log(this.data.courses_datas)
  },
  //筛选出不同tag的内容
  select:function (e) {
    this.setData({
      showViews: (!this.data.showViews),
    })
    let that = this
    db.collection('Study_Information')
    .where({
      type:"课程推荐",
      tag:e,
      title:db.RegExp({
        regexp: '.*' + that.data.title + '.*',//做为关键字进行匹配
        options: 'i',//不区分大小写
      })
    })
    .get({
      success: function(res) {
        if(res.data.length != 0){
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:false
          })
        }else{
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:true
          })
        }
      }
    })
  },
  select_1:function () {
    this.select("考研")
  },
  select_2:function () {
    this.select("考公")
  },
  select_3:function () {
    this.select("专业课")
  },
  select_4:function () {
    this.select("基础学科")
  },
  select_5:function () {
    this.setData({
      showViews: (!this.data.showViews),
    })
    let that = this
    db.collection('Study_Information').where({
      type:'课程推荐'
    }).get({
      success: function(res) {
        if(res.data.length != 0){
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:false
          })
        }else{
          that.setData({
            is_empty:true
          })
        }
      }
    })
  },
  search_hot:function (e) {
    for(let i = 0;i < this.data.courses_datas.length;i++){ 
      if(this.data.courses_datas[i]._id === e){
        return this.data.courses_datas[i].hot + 1
      }
    }
    return -1
  },
  //跳转详情页面
  link_content:function (e) {
    app.globalData.study_index = e.currentTarget.id
    let hot = this.search_hot(e.currentTarget.id)
    db.collection('Study_Information')
    .doc(e.currentTarget.id)
    .update({
      data:{
        hot:hot
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let that = this
    db.collection('Study_Information').where({
      type:'课程推荐'
    }).get({
      success: function(res) {
        if(res.data.length != 0){
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:false
          })
        }else{
          that.setData({
            course_datas:res.data,
            courses_datas:res.data,
            is_empty:true
          })
        }
      }
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})